/**
 * 自定义原创案例
 * */

var formList = [];
var cateAry = [];


//列表
function getOption(){
    $.ajax({
        type: apis.caseCateOption.method,
        url: apis.caseCateOption.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            cateAry = res.data.cate;
            var h = '', i;
            h += '<option value="">';
            h += '所有';
            h += '</option>';
            for (i = 0; i < cateAry.length; i ++) {
                if (pid == cateAry[i].id) {
                    h += '<option value="' + cateAry[i].id + '" selected>';
                } else {
                    h += '<option value="' + cateAry[i].id + '">';
                }
                h += cateAry[i].title;
                h += '</option>';
            }
            $('#pid').html(h);
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


//列表
function getIndex(v){
    var formData = {
        pid: v,
    };
    $.ajax({
        type: apis.caseCateIndex.method,
        url: apis.caseCateIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            formList = res.data;
            indexHtml();
        },
        error: function () {
            console.log('错误啦');
        }
    });
}


//列表字符串
function indexHtml(){
    if (formList.length == 0) {
        msgPop({msg: '没有数据'}); return
    }
    var d = formList;
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        //h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].pname + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="case_edit(\'编辑\',\'case-cate-edit.html\', ' + d[i].id + ')">';
        h += '修改';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="case_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        h += '</a>';
        h += '<input type="hidden" id="cateid' + d[i].id + '" value="' + d[i].cateid + '"/>';
        h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="pid' + d[i].id + '" value="' + d[i].pid + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//编辑弹窗
function case_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    var pid = $('#pid' + id).val();
    var cateid = $('#pid' + cateid).val();
    tt = tt ? tt : '';
    url += '&title=' + escape(tt);
    url += '&pid=' + pid;
    url += '&cateid=' + cateid;
    layer_show(title,url,w,300);
}


//保存
function modify(){
    var img = $('#img').val();
    var formData = {
        id: id,
        title: $('#title').val(),
        pid: $('#pid').val(),
    };
    if (!formData.title) {
        msgPop({msg: '项目名称必须的'}); return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.caseCateModify.method,
        url: apis.caseCateModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '更新成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//删除
function case_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.caseCateDel.method,
            url: apis.caseCateDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}